package com.shsmi.dsn.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.shsmi.dsn.dao.DsnExamineDictRepository;
import com.shsmi.dsn.dao.DsnExamineRepository;
import com.shsmi.dsn.dao.DsnTaskRepository;
import com.shsmi.dsn.domain.DsnExamine;
import com.shsmi.dsn.domain.DsnExamineDict;
import com.shsmi.dsn.domain.DsnTask;
import com.shsmi.dsn.service.ExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ExamineServiceImpl implements ExamineService {
    @Autowired
    private DsnExamineRepository repository;

    @Autowired
    private DsnExamineDictRepository dictRepository;

    @Autowired
    private DsnTaskRepository taskRepository;

    @Override
    public List<DsnExamine> list(String classify, String examineTime) {
        List<DsnExamine> examineList = repository.findByClassifyAndAndExamineTime(classify,examineTime);

        Calendar calendar = Calendar.getInstance();
        Date date = DateUtil.parse(examineTime+"-01"+" 00:00:00");
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Long start = calendar.getTimeInMillis();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Long end = calendar.getTimeInMillis();
        BigDecimal sumAll = BigDecimal.ZERO;
        for(DsnExamine examine : examineList){

            if(examine.getClassifyFirstScore().compareTo(BigDecimal.valueOf(-1))==0){
                BigDecimal sum = BigDecimal.ZERO;
                String keyword = examine.getClassify()+"/"+examine.getClassifyFirst()+"%";
                List<DsnTask> taskList =  taskRepository.findByAndGradeCodeLikeAndInspectTimeGreaterThanEqualAndInspectTimeLessThanEqual(keyword,start.toString(),end.toString());
                for(DsnTask task: taskList){
                    if(task.getCurrentState()<4){
                        sum = sum.add(task.getFinalScore().multiply(task.getGradeWeight()));
                    }
                }
                examine.setClassifyFirstScore(BigDecimal.valueOf(100).subtract(sum));
            }

            sumAll = sumAll.add(examine.getClassifyFirstScore().multiply(examine.getClassifyFirstWeight()));
        }
        DsnExamine sumExamine = new DsnExamine();
        sumExamine.setClassify(classify);
        sumExamine.setClassifyFirst("合计");
        sumExamine.setExamineTime(examineTime);
        sumExamine.setClassifyFirstScore(sumAll);
        examineList.add(sumExamine);
        return examineList;
    }

    @Override
    public DsnExamine getById(Long id) {
        return repository.findById(id).get();
    }

    @Override
    public DsnExamine insert(DsnExamine record) {
        return repository.save(record);
    }

    @Override
    public List<DsnExamine> insert(String classify, String examineTime) {
        List<DsnExamine> resultList = new ArrayList<>();
        List<DsnExamineDict> examineDictList = dictRepository.findByClassify(classify);

        for(DsnExamineDict record: examineDictList){
            DsnExamine examine = new DsnExamine();
            examine.setExamineTime(examineTime);
            examine.setClassify(record.getClassify());
            examine.setClassifyFirst(record.getClassifyFirst());
            examine.setClassifyFirstWeight(record.getClassifyFirstWeight());

            if(record.getCalculate()){
                examine.setClassifyFirstScore(BigDecimal.valueOf(-1));
            }else{
                examine.setClassifyFirstScore(record.getInitialScore());
            }
            examine.setPushed(false);
            repository.save(examine);
            resultList.add(examine);
        }

        return resultList;
    }

    @Override
    public DsnExamine update(DsnExamine record) {
        return repository.save(record);
    }

    @Override
    public List<DsnExamine> update(List<DsnExamine> recordList) {
        List<DsnExamine> resultList = new ArrayList<>();
        for (DsnExamine record : recordList){
            repository.save(record);
            resultList.add(record);
        }
        return resultList;
    }

    @Override
    public void delete(Long id) {
        repository.deleteById(id);
    }
}
